﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Game.Platforms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game.UIScripts
{
    class GameScreen : Panel
    {
        private Level _level;

        private readonly UIController _ui;
        private PauseMenu _pauseMenu;
        private LevelComplete _levelComplete;

        private readonly IServiceProvider _serviceProvider;
        private readonly ContentManager _contentManager;
        private readonly GraphicsDevice _graphicsDevice;
        private MouseState _previousMouseState;

        private Texture2D _hud;
        private bool _ignoreClick;

        private readonly List<UIButton> _buttons = new List<UIButton>();
        private int _selectedButtonIndex = -1;
        private string _selectedButtonName;

        private SpriteFont _fontTimer;
        private SpriteFont _fontLive;

        private string _timer;
        private double _startTime;
        public double TotalTime;

        private double _timeCheck = 0;

        public int Money = 0;

        private bool _isPaused = false;
        private bool _isAlive = false;
        private bool _isWin = false;

        private string _newLevel = "";

        public GameScreen(IServiceProvider serviceProvider, GraphicsDevice graphicsDevice, ContentManager content, UIController controller, string level)
        {
            _contentManager = content;
            _graphicsDevice = graphicsDevice;
            _serviceProvider = serviceProvider;
            _ui = controller;
            _timer = "";
            _level = new Level(serviceProvider, level, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
            _pauseMenu = new PauseMenu(serviceProvider, graphicsDevice, content, controller, this);
            _levelComplete = new LevelComplete(serviceProvider, graphicsDevice, content, controller, this);
            if(_ui.PlayLevel.Equals("test1"))
                _levelComplete.Reset();
            _ui.PlayLevel = level;
            LoadContent();
        }

        public void Reset()
        {
            _isPaused = false;
            _ignoreClick = false;
            _timer = "";
            _startTime = 0;
            TotalTime = 0;
            _selectedButtonIndex = -1;
            Money = 0;
            _isAlive = false;
            _isWin = false;
            _timeCheck = 0;
            if(!_newLevel.Equals(""))
            {
                _level = new Level(_serviceProvider, _newLevel, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height);
                _newLevel = "";
            }
            else
            {
                _level.Unload();
                _level = new Level(_serviceProvider, _level.LevelName, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height);
            }
                
            _pauseMenu = new PauseMenu(_serviceProvider, _graphicsDevice, _contentManager, _ui, this);
            _levelComplete = new LevelComplete(_serviceProvider, _graphicsDevice, _contentManager, _ui, this);
        }

        public void Reset(string level)
        {
            if (level.Equals("test9"))
                level = "test1";
            _ui.PlayLevel = level;
            _newLevel = level;
            Reset();
        }

        private void LoadContent()
        {
            _hud = _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/GAMEHUD");
            _fontTimer = _contentManager.Load<SpriteFont>("Sprites/UI/GameScreen/timer");
            _fontLive = _contentManager.Load<SpriteFont>("Sprites/UI/GameScreen/live");
            _buttons.Add(new UIButton("PAUSE", new Vector2(1181, 5), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/PauseButton"), true));
            _buttons.Add(new UIButton("Flat", new Vector2(1181, 300), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnNormal"), true));
            _buttons.Add(new UIButton("Elevator", new Vector2(1181, 400), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnElevator"), true));
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState)
        {
            if(!_isPaused && !_isAlive && !_isWin)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    _isPaused = true;
                    _ignoreClick = true;

                }

                _level.Update(gameTime, keyboardState, mouseState);
                Money = _level.Score;
                if (!_level.Player.IsAlive)
                {
                    PlayerDied(gameTime);
                }
                else if(_level.ReachedExit)
                {
                    PlayerWin(gameTime);
                }
                else
                {
                    if (!(_startTime > 1))
                    {
                        _startTime = gameTime.TotalGameTime.TotalSeconds;
                    }
                    else
                    {
                        TotalTime = (gameTime.TotalGameTime.TotalSeconds - _startTime);
                        _timer = TotalTime.ToString("0.0");
                    }

                    if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Released)
                    {
                        //When clicked
                        int counter = 0;
                        foreach (UIButton btn in _buttons)
                        {
                            if (checkCollision(new Vector2(mouseState.X, mouseState.Y), btn))
                            {
                                _previousMouseState = mouseState;
                                Console.WriteLine("Clicked");
                                switch (btn.Name)
                                {
                                    case "PAUSE":
                                        //PAUSE METHOD
                                        _isPaused = true;

                                        _ignoreClick = true;
                                        Console.WriteLine("PAUSED");
                                        break;
                                    default:
                                        if (!_ignoreClick)
                                        {
                                            _selectedButtonName = btn.Name;
                                            _buttons.Add(new UIButton("NEW", btn.Position, _contentManager.Load<Texture2D>("Sprites/Platforms/" + btn.Name), true));
                                            for (int x = 0; x < _buttons.Capacity; x++)
                                            {
                                                if (_buttons[x].Name.Equals("NEW"))
                                                {
                                                    _selectedButtonIndex = x;
                                                    break;
                                                }
                                            }
                                        }
                                        break;
                                }
                                break;
                            }
                        }
                    }
                    else if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed)
                    {
                        Console.WriteLine("Released");
                        //When released
                        if (_selectedButtonIndex != -1)
                        {

                            switch (_selectedButtonName)
                            {
                                case "Flat":
                                    _level.AddPlatform(PlatformType.Flat, mouseState.X, mouseState.Y);
                                    break;
                                case "Elevator":
                                    _level.AddPlatform(PlatformType.Elevator, mouseState.X, mouseState.Y);
                                    break;
                            }
                            
                            
                            _buttons.RemoveAt(_selectedButtonIndex);
                            _selectedButtonIndex = -1;
                        }

                        _previousMouseState = mouseState;
                        _ignoreClick = false;
                    }
                    else if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Pressed)
                    {
                        if (_selectedButtonIndex != -1)
                            _buttons[_selectedButtonIndex].SetPosition(new Vector2(mouseState.X - _buttons[_selectedButtonIndex].Texture.Width / 2, mouseState.Y - _buttons[_selectedButtonIndex].Texture.Height / 2));
                    }
                }
            }
            else if(_isPaused)
            {
                _startTime = gameTime.TotalGameTime.TotalSeconds - TotalTime;
                _pauseMenu.Update(gameTime,keyboardState,mouseState);
            }
            else if(_isAlive)
            {
                Reset();
            }
            else if (_isWin)
            {
                _levelComplete.Update(gameTime, keyboardState, mouseState);
            }
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _graphicsDevice.Clear(Color.Black);
            _level.Draw(gameTime, spriteBatch);
            //spriteBatch.Draw(_texture);
            spriteBatch.Begin();
            spriteBatch.Draw(_hud,new Vector2(0,0), Color.White);
            foreach (UIButton btn in _buttons)
            {
                spriteBatch.Draw(btn.Texture, btn.Position, Color.White);
            }
            spriteBatch.DrawString(_fontLive, _ui.PlayLevel, new Vector2(14, 10), Color.White);

            //spriteBatch.DrawString(_fontLive, "$"+Money, new Vector2(14, 10), Color.White);
            spriteBatch.DrawString(_fontTimer, _timer + "s", new Vector2(549.5f, 3), Color.White);
            spriteBatch.End();
            if (_isPaused)
                _pauseMenu.Draw(gameTime, spriteBatch);
            if (_isWin)
                _levelComplete.Draw(gameTime, spriteBatch);
        }

        private bool checkCollision(Vector2 mousePos, UIButton btn)
        {
            if (mousePos.X >= btn.Position.X // To the right of the left side
            && mousePos.X < btn.Position.X + btn.Texture.Width //To the left of the right side
            && mousePos.Y > btn.Position.Y //Below the top side
            && mousePos.Y < btn.Position.Y + btn.Texture.Height) //Above the bottom side
                return true; //We are; return true.
            return false; //We're not; return false.
        }

        public void Resume()
        {
            _isPaused = false;
        }

        private void PlayerDied(GameTime gameTime)
        {
            _buttons.Clear();
            _buttons.Add(new UIButton("PAUSE", new Vector2(1181, 5), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/PauseButton"), true));
            _buttons.Add(new UIButton("Flat", new Vector2(1181, 300), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnNormal"), true));
            _buttons.Add(new UIButton("Elevator", new Vector2(1181, 400), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnElevator"), true));
            if(!(_timeCheck > 0))
            {
                _timeCheck = gameTime.TotalGameTime.TotalSeconds;
            }
            else if(gameTime.TotalGameTime.TotalSeconds > _timeCheck + 1.3)
            {
                _isAlive = true;
            }
        }

        private void PlayerWin(GameTime gameTime)
        {
            _buttons.Clear();
            _buttons.Add(new UIButton("PAUSE", new Vector2(1181, 5), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/PauseButton"), true));
            _buttons.Add(new UIButton("Flat", new Vector2(1181, 300), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnNormal"), true));
            _buttons.Add(new UIButton("Elevator", new Vector2(1181, 400), _contentManager.Load<Texture2D>("Sprites/UI/GameScreen/SpawnElevator"), true));
            if (!(_timeCheck > 0))
            {
                _timeCheck = gameTime.TotalGameTime.TotalSeconds;
            }
            else if (gameTime.TotalGameTime.TotalSeconds > _timeCheck + 1.3)
            {
                _isWin = true;
            }
        }
    }
}
